const wi = wx.getWindowInfo();


function rpxToPx(rpx) {
    let deviceWidth = wi.windowWidth; //获取设备屏幕宽度
    let px = (deviceWidth / 750) * rpx;
    return Math.floor(px);
}

function pxToRpx(px) {
    let deviceWidth = wi.windowWidth; //获取设备屏幕宽度
    let rpx = (750 / deviceWidth) * px;
    return Math.floor(rpx);
}

// 获取顶部状态栏的高度
const statusBarHeight = wi.statusBarHeight;

const clientRect = () => {
    let rect = wx.getMenuButtonBoundingClientRect();
    if (!rect) {
        rect = { bottom: 56, height: 32, left: 278, right: 365, top: 24, width: 87 };
    }
    return rect;
};
// 获取顶部菜单栏的高度
const navbarHeight = () => {
    const d = clientRect();
    return d.height + (d.top - statusBarHeight) * 2;
};
// 获取菜单栏可用区宽度
const navbarScreenWidth = () => {
    const rect = clientRect();
    return rect.left;
}
// 整个顶部被占用高度
const topHeight = () => {
    return statusBarHeight + navbarHeight();
}
// 获取底部安全区域高度
const bottomSafeHeight = () => {
    if (wi.safeArea){
        return wi.screenHeight - wi.safeArea.bottom;
    }else {
        return 5; // 暂时没有适配
    }
}
// 获取屏幕可控制区域高度
//  屏幕高度减去顶部状态栏高度减去底部安全区域高度
const screenHeight = (bottom = true) => {
    const i = wi.screenHeight - topHeight();
    if (bottom) {
        return i - bottomSafeHeight();
    }else {
        return i;
    }
}

module.exports = {
    rpxToPx,
    pxToRpx,
    statusBarHeight,
    navbarHeight,
    navbarScreenWidth,
    topHeight,
    bottomSafeHeight,
    screenHeight,
    clientRect
}
